package com.example.smartcity4.GuidePager;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.smartcity4.MainActivity;
import com.example.smartcity4.R;
import com.example.smartcity4.adapter.GuidePagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class GuidePagerActivity2 extends AppCompatActivity implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {

    private ViewPager viewPager;
    private RadioButton item1;
    private RadioButton item2;
    private RadioButton item3;
    private RadioButton item4;
    private RadioButton item5;
    private RadioGroup radio;
    private List<View> list;
    private Button setIp;
    private Button goHome;

    private GuidePagerAdapter guidePagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_pager2);
        initView();

    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        item1 = (RadioButton) findViewById(R.id.item1);
        item2 = (RadioButton) findViewById(R.id.item2);
        item3 = (RadioButton) findViewById(R.id.item3);
        item4 = (RadioButton) findViewById(R.id.item4);
        item5 = (RadioButton) findViewById(R.id.item5);
        radio = (RadioGroup) findViewById(R.id.radio);

        setIp = findViewById(R.id.setIp);
        goHome = findViewById(R.id.goHome);

        initData();
        initAdapter();
    }



    private void initData() {
        list = new ArrayList<>();
        list.add(setView(R.layout.guidepager1));
        list.add(setView(R.layout.guidepager2));
        list.add(setView(R.layout.guidepager3));
        list.add(setView(R.layout.guidepager4));
        list.add(setView(R.layout.guidepager5));
        initStart();

    }
    private void initAdapter() {
        guidePagerAdapter = new GuidePagerAdapter(list);
        viewPager.setAdapter(guidePagerAdapter);
        viewPager.setCurrentItem(0);
        item1.setChecked(true);
        viewPager.addOnPageChangeListener(this);
        radio.setOnCheckedChangeListener(this);
    }

    private View setView(int i) {
        return LayoutInflater.from(this).inflate(i, null);
    }

    private void initStart() {
        setIp = list.get(4).findViewById(R.id.setIp);
        setIp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                set_ip();
            }


        });
        goHome = list.get(4).findViewById(R.id.goHome);
        goHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), MainActivity.class));
                GuidePagerActivity2.this.finish();
            }
        });

    }

    private void set_ip() {

        View view = view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.ip_item, null);
        final EditText ip = view.findViewById(R.id.get_ip);
        final EditText math = view.findViewById(R.id.get_math);
        new AlertDialog.Builder(this)
                .setTitle("设置网络")
                .setView(view)
                .setNegativeButton("取消", null)
                .setPositiveButton("保存", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (TextUtils.isEmpty(math.getText().toString().trim())) {
                            Toast.makeText(getApplicationContext(), "端口号不能为空", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (!ip.getText().toString().contains(".")) {
                            Toast.makeText(getApplicationContext(), "IP地址错误", Toast.LENGTH_SHORT).show();
                        } else {
                            SharedPreferences sharedPreferences = getSharedPreferences("SP", MODE_PRIVATE);
                            sharedPreferences.edit().putString("host", "http://" + ip.getText().toString() + ":" + math.getText().toString()).apply();
                            MainActivity.host = "http://" + ip.getText().toString() + ":" + math.getText().toString();

                        }
                    }
                }).show();

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
switch (position){
    case 0 :
        item1.setChecked(true);
        break;
    case 1:
        item2.setChecked(true);
        break;
    case 2:
        item3.setChecked(true);
        break;
    case 3:
        item4.setChecked(true);
        break;
    case 4:
        item5.setChecked(true);
        break;
}
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
    case R.id.item1:
        viewPager.setCurrentItem(0);
        break;
    case R.id.item2:
        viewPager.setCurrentItem(1);
        break;
    case R.id.item3:
        viewPager.setCurrentItem(2);
        break;
    case R.id.item4:
        viewPager.setCurrentItem(3);
        break;
    case R.id.item5:
        viewPager.setCurrentItem(4);
        break;
}
    }
}